// active/send-active/send-active.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';
import notice from '../../utils/notice.js';

const app = getApp();
let pageIndex = 1;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // style: app.globalData.style,
        // height: 0,
        list: [],
        isRefresh: false,
        isOptions: false,
        visibleCancel: false,
        visibleOnShelves: false,
        visibleOffShelves: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.name = '';
        wx.startPullDownRefresh();

        notice.addNotice("RefreshActivePart", this.onRefresh, this);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // const query = wx.createSelectorQuery().in(this);
        // query.select("#head").boundingClientRect((data) => {
        //     let windowInfo = wx.getWindowInfo();
        //     this.setData({
        //         height: windowInfo.windowHeight - data.height,
        //         statusBarHeight: windowInfo.statusBarHeight
        //     })
        // }).exec();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("RefreshActivePart", this);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            isRefresh: true
        })
        this.hasData = true;
        pageIndex = 1;
        this.request();
    },

    onRefresh() {
        this.onPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.hasData) {
            this.request();
        }
    },

    // onSearch(e) {
    //     this.name = e.detail.value
    //     pageIndex = 1;
    //     this.request();
    // },

    // onClear() {
    //     this.name = '';
    //     pageIndex = 1;
    //     this.request();
    // },
    /**
     * 请求数据 
     * 将每一页的数据放一个数组，即形成一个二维数组,减少页面渲染的次数
     */
    request: function () {
        let param = {
            pageNo: pageIndex,
            pageSize: 20,
            queryType: 1,
            creator: getApp().globalData.userInfo.id
        }

        network.getJson(api.activePage, param).then(res => {
            this.setData({
                isRefresh: false
            })
            if (pageIndex == 1) {
                this.setData({
                    list: []
                })
            }
            let list = res.data.list;

            for (let i = 0; i < list.length; i++) {
                if (list[i].images) {
                    list[i].images = list[i].images.split(',');
                }
                list[i].createTime = formatDate.formatTime(list[i].createTime, 'yyyy.MM.dd HH:mm');
                list[i].isOptions = false;
            }
            this.setData({
                list: this.data.list.concat(list)
            })
            if (this.data.list.length < res.data.total) {
                this.hasData = true;
                pageIndex += 1;
            } else {
                this.hasData = false;
            }
        })
    },

    goDetails(e) {
        const {
            id
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/active/active-details/active-details?id=' + id
        })
    },

    optionsTap(e) {
        this.index = e.currentTarget.dataset.index;
        this.setData({
            isOptions: true,
            [`list[${this.index}].isOptions`]: true
        })
    },

    closeTap() {
        if (this.data.isOptions) {
            this.setData({
                isOptions: false,
                [`list[${this.index}].isOptions`]: false
            })
        }
    },

    offShelvesTap() {
        this.setData({
            visibleOffShelves: true,
            isOptions: false
        })
    },

    confirmOffShelves() {
        this.setData({
            visibleOffShelves: false
        })
        let that = this;
        let active = this.data.list[this.index]
        network.postJson(api.updateActive, {
            id: active.id,
            onShelves: 2
        }).then(res => {
            wx.showToast({
                icon: 'none',
                title: '操作成功',
                complete: function () {
                    notice.postNotice("ActiveRefresh");
                    that.onRefresh();
                }
            })
        })
    },

    onShelvesTap() {
        this.setData({
            visibleOnShelves: true,
            isOptions: false
        })
    },

    confirmOnShelves() {
        this.setData({
            visibleOnShelves: false
        })
        let that = this;
        let active = this.data.list[this.index]
        network.postJson(api.updateActive, {
            id: active.id,
            onShelves: 1
        }).then(res => {
            wx.showToast({
                icon: 'none',
                title: '操作成功',
                complete: function () {
                    notice.postNotice("ActiveRefresh");
                    that.onRefresh();
                }
            })
        })
    },

    cancelActiveTap() {
        this.setData({
            visibleCancel: true,
            isOptions: false
        })
    },

    confirmCancel() {
        let ids = this.data.list[this.index].timerIds;
        if (!ids || ids.length == 0) {
            wx.showToast({
                icon: 'none',
                title: '没有可取消的场次'
            })
            return;
        }
        network.postJson(api.doCancelOrderTimer, {
            ids: ids.join(',')
        }, 'application/x-www-form-urlencoded').then(res => {
            wx.showToast({
                icon: 'none',
                title: '取消成功'
            })
            this.data.list.splice(this.index, 1);
            this.setData({
                list: this.data.list
            })
        })
    },

    editTap(e) {
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/active/publish/publish?active=' + encodeURIComponent(JSON.stringify(item))
          })
    }

})